package com.easygoods.controller;

import com.bestvike.stone.spring.controller.BaseController;
import com.bestvike.stone.spring.page.PageableRequest;
import com.bestvike.stone.spring.page.PageableResponse;
import com.bestvike.stone.spring.rest.common.CommonResponse;
import com.bestvike.stone.spring.shiro.annotation.RequiresPermissions;
import com.easygoods.request.StarSpuRequest;
import com.easygoods.response.SpuListEarnRateResponse;
import com.easygoods.response.SpuListResponse;
import com.easygoods.response.StarSpuResponse;
import com.easygoods.service.StoreService;
import com.easygoods.shiro.auth.UserPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by 许崇雷 on 2019-01-08.
 */
@RestController
@RequestMapping(value = "/api/store")
public class StoreController extends BaseController {
    @Autowired
    private StoreService storeService;

    //收藏,不会返回失败
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping(value = "/starSpu")
    public CommonResponse<StarSpuResponse> starSpu(@RequestBody StarSpuRequest request) {
        StarSpuResponse starSpuResponse = this.storeService.starSpu(request);
        return CommonResponse.success(starSpuResponse);
    }

    //取消收藏
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping(value = "/unstarSpu")
    public CommonResponse<Object> unstarSpu(@RequestBody StarSpuRequest request) {
        this.storeService.unStarSpu(request);
        return CommonResponse.success();
    }

    //查询用户收藏的spu列表
    @RequiresPermissions(UserPermissions.BUY)
    @PostMapping("/getStoreSpuList")
    public CommonResponse<PageableResponse<SpuListResponse>> getStoreSpuList(@RequestBody PageableRequest pageableRequest) {
        PageableResponse<SpuListResponse> pageableResponse = this.storeService.getStoreSpuList(pageableRequest);
        return CommonResponse.success(pageableResponse);
    }

    //在收藏的商品中查询收益率最高的两种商品
    @RequiresPermissions(UserPermissions.BUY)
    @GetMapping("/getStoreSpuListWithEarnRate")
    public CommonResponse<List<SpuListEarnRateResponse>> getStoreSpuListWithEarnRate() {
        final List<SpuListEarnRateResponse> spuListResponseList = this.storeService.getStoreSpuListWithEarnRate();
        return CommonResponse.success(spuListResponseList);
    }
}
